1 00:00:00,980 --> 00:00:06,320 All right, before we continue setting up the storyline for our game, I want to add some features to 2 00:00:06,320 --> 00:00:07,580 our players characters. 3 00:00:07,580 --> 00:00:13,040 First, one of these features we can do to improve the ambiance in our game is to add footsteps to the 4 00:00:13,040 --> 00:00:15,560 player, and this is relatively easy to do. 5 00:00:15,560 --> 00:00:20,240 All we need to do is fill out our local script that we have inside of starters character scripts that 6 00:00:20,240 --> 00:00:23,660 will be responsible for handling all of the character's footsteps. 7 00:00:24,020 --> 00:00:27,950 Now, if you remember inside of our player, join service. 8 00:00:27,950 --> 00:00:33,890 When a player's character gets added, we add a new instance or sound instance into that player's root 9 00:00:33,890 --> 00:00:35,780 part, and it's called footsteps. 10 00:00:35,780 --> 00:00:42,590 And this is the sound instance that we're going to be using to replace the default footstep sound that 11 00:00:42,590 --> 00:00:44,450 exists within a player's character. 12 00:00:44,450 --> 00:00:50,240 There's another sound instance which is called a running underneath a player's humanoid root part. 13 00:00:50,240 --> 00:00:57,020 And we want to disable that sound and instead add functionality for our custom footstep sound so we 14 00:00:57,020 --> 00:00:59,510 can go inside of our footsteps handler. 15 00:00:59,600 --> 00:01:02,330 And in here we're only going to need two services. 16 00:01:02,330 --> 00:01:04,520 One is going to be replicated storage. 17 00:01:07,330 --> 00:01:10,000 And then the other service we'll need is the player service. 18 00:01:13,820 --> 00:01:19,490 Now, the first variable we'll need is a reference to a module script we have inside of replicated storage. 19 00:01:19,490 --> 00:01:22,310 So inside of here we have a footstep module. 20 00:01:22,310 --> 00:01:24,320 And this was created by another developer. 21 00:01:24,320 --> 00:01:32,360 And what it does is that it stores a bunch of different sound IDs depending on the surface a player 22 00:01:32,360 --> 00:01:33,170 is walking on. 23 00:01:33,170 --> 00:01:37,940 So we have stuff for like sand, snow, tile, wood and things of that. 24 00:01:37,940 --> 00:01:39,830 And there's a material map in here. 25 00:01:39,830 --> 00:01:44,990 So what we do is we pass a particular enum material that a player is walking on. 26 00:01:44,990 --> 00:01:49,760 And from that we get a table of sounds that would exist for that particular material. 27 00:01:49,760 --> 00:01:56,810 And then we get those sounds and we play a random one of those sound IDs for this particular floor material. 28 00:01:57,600 --> 00:02:00,480 So we can go ahead and require this module script. 29 00:02:00,480 --> 00:02:05,790 We'll call it footstep module require from replicated storage dot modules. 30 00:02:05,790 --> 00:02:07,200 Dot footstep module. 31 00:02:07,930 --> 00:02:10,660 And then we want to make a reference to the local player. 32 00:02:10,720 --> 00:02:13,150 So players dot local player. 33 00:02:13,150 --> 00:02:15,730 And then we want to get a reference to the player's character. 34 00:02:15,880 --> 00:02:22,900 So we could do player dot character or we can do player dot character added weight in case the player's 35 00:02:22,990 --> 00:02:24,910 character hasn't been replicated yet. 36 00:02:25,000 --> 00:02:28,000 And then we want to get a reference to the humanoid in our character. 37 00:02:28,000 --> 00:02:31,540 So character weight for child humanoid. 38 00:02:33,230 --> 00:02:36,320 And then we want to get a reference to the root part of the player. 39 00:02:36,320 --> 00:02:38,930 So character wait for child. 40 00:02:38,930 --> 00:02:40,700 Humanoid root part. 41 00:02:41,680 --> 00:02:46,870 And then I'm going to create a variable to keep track of the last material that our player was standing 42 00:02:46,870 --> 00:02:48,850 on, so we can call it last material. 43 00:02:48,850 --> 00:02:50,770 And for now we're going to set it to nil. 44 00:02:51,160 --> 00:02:54,220 Now we're going to make some references to the sounds in the player. 45 00:02:54,220 --> 00:02:59,650 So one is going to be the default running sound, which is on our root part. 46 00:02:59,650 --> 00:03:02,320 And we're going to wait for the running sound. 47 00:03:02,320 --> 00:03:06,010 And then we also want to make a reference to our custom footstep sound. 48 00:03:07,560 --> 00:03:11,730 Which is also in our root part, and its name is footsteps. 49 00:03:12,810 --> 00:03:18,600 Now, some other things we're going to want to look at is we want to get the walk speed of our character. 50 00:03:18,600 --> 00:03:21,090 So walk speed is equal to our humanoid. 51 00:03:21,090 --> 00:03:23,160 And we'll get the walk speed property. 52 00:03:24,010 --> 00:03:28,300 And then we'll have a variable to keep track of our current walk speed. 53 00:03:28,570 --> 00:03:30,430 And we'll set that to zero for now. 54 00:03:30,580 --> 00:03:35,380 We're going to have a variable that will act as a cooldown, because we're going to have a while loop 55 00:03:35,380 --> 00:03:37,660 that will constantly check if we are walking. 56 00:03:37,660 --> 00:03:43,090 And if we're walking it'll play a footstep sound, but we don't want to constantly play a footstep sound. 57 00:03:43,090 --> 00:03:45,580 We want to play a footstep sound. 58 00:03:45,580 --> 00:03:47,830 Depending on how fast our player is walking. 59 00:03:47,830 --> 00:03:53,920 And by default, we're going to have a cooldown of 0.35 seconds between each footstep sound. 60 00:03:53,950 --> 00:03:59,260 And then last but not least, we're going to have a volume for our footstep sounds as well, and we'll 61 00:03:59,260 --> 00:04:01,000 just set that to something like 0.3. 62 00:04:01,570 --> 00:04:09,340 And the last but not least, what we want to do is we want to set the running sound dot volume equal 63 00:04:09,340 --> 00:04:15,550 to zero, because we don't want to worry about Roblox's default implementation of the running sound. 64 00:04:15,550 --> 00:04:18,460 We want to go ahead and make sure it can't be heard at all. 65 00:04:19,060 --> 00:04:21,520 Now, some private functions we're going to need in here. 66 00:04:21,880 --> 00:04:26,230 Uh, one we need is to check the movement of our player. 67 00:04:26,230 --> 00:04:32,410 And then another function we're going to need is to get the material that the player is standing on. 68 00:04:32,410 --> 00:04:38,050 And from that, we can return a table containing all of the different sound IDs based on that material. 69 00:04:38,050 --> 00:04:42,700 So we can call this get material, and we'll go ahead and do that stuff in there. 70 00:04:42,700 --> 00:04:48,940 Now another thing we want to listen in the handler section is when the walk speed of our player changes. 71 00:04:48,940 --> 00:04:54,130 So we can do humanoid, get property, change signal walk speed. 72 00:04:56,360 --> 00:05:01,910 And when this walks be changes, we want to update our cool down to a different number. 73 00:05:01,910 --> 00:05:05,360 So for example, if the humanoids walk speed. 74 00:05:06,250 --> 00:05:10,210 Is greater than the original walk speed we stored in that variable. 75 00:05:10,210 --> 00:05:14,680 Then we want to set our cooldown to be something shorter, so that way we can play footsteps faster 76 00:05:14,680 --> 00:05:16,780 so we can set it to something like 0.25. 77 00:05:17,860 --> 00:05:26,560 Otherwise, if our walk speed is less than our original walk speed, then we want to reduce the cooldown 78 00:05:26,560 --> 00:05:30,010 so we could set it to something like 0.45. 79 00:05:30,720 --> 00:05:37,710 Otherwise, if our walk speed stayed the same, then we can just keep our cooldown at 0.35, which is 80 00:05:37,710 --> 00:05:39,480 the default we set up here. 81 00:05:40,110 --> 00:05:44,520 Another event we want to listen to is when our humanoid actually starts to move, and there's an event 82 00:05:44,520 --> 00:05:48,900 in our humanoid called running, and this gets fired when the player starts to move. 83 00:05:48,900 --> 00:05:53,490 So we're going to connect a function to this, and this function gets passed an integer that represents 84 00:05:53,490 --> 00:05:54,840 the speed of the player. 85 00:05:55,810 --> 00:06:00,820 And all we're going to do in here is we're going to set the current speed variable equal to this speed. 86 00:06:01,180 --> 00:06:01,510 All right. 87 00:06:01,510 --> 00:06:04,960 So now in our main section here we're going to create a while loop. 88 00:06:04,960 --> 00:06:11,770 So while true what we're going to do is we're going to wait for our cooldown. 89 00:06:12,280 --> 00:06:18,580 And then after that's up we're going to check if our player is moving using our check movement private 90 00:06:18,580 --> 00:06:19,300 function. 91 00:06:19,300 --> 00:06:22,960 So we can have this function return a boolean that will tell us whether or not the player is moving. 92 00:06:22,960 --> 00:06:27,550 So if check movement is true then we can play a footstep sound. 93 00:06:27,550 --> 00:06:29,650 So let's go ahead and fill out this function here. 94 00:06:29,650 --> 00:06:35,740 So what we're going to do is we're going to set the last material variable equal to the humanoid's current 95 00:06:35,740 --> 00:06:36,550 floor material. 96 00:06:36,550 --> 00:06:39,370 So there's a property called floor material. 97 00:06:39,460 --> 00:06:44,860 And actually if I go up here where I get the human I can denote this as a humanoid. 98 00:06:44,860 --> 00:06:49,750 And that way we can actually see all of the different properties and stuff for the humanoid. 99 00:06:49,750 --> 00:06:54,280 So this will tell us the current floor material that the humanoid is standing on. 100 00:06:54,730 --> 00:06:59,470 And then after that, what we're going to do is we're going to check if the humanoid dot walk speed 101 00:06:59,530 --> 00:07:01,210 is greater than zero. 102 00:07:01,600 --> 00:07:08,380 And we also want to make sure that the humanoid dot move direction describes the direction that the 103 00:07:08,380 --> 00:07:09,340 humanoid is walking in. 104 00:07:09,340 --> 00:07:14,440 We want to make sure that the magnitude of this move direction is greater than zero, because if it 105 00:07:14,440 --> 00:07:16,990 isn't greater than zero, then the player isn't moving. 106 00:07:17,810 --> 00:07:24,800 And then we also want to check whether or not the current speed of the player is also greater than zero. 107 00:07:25,280 --> 00:07:34,670 Another condition we'll want to check is if the last material we are standing on is not equal to nil. 108 00:07:34,670 --> 00:07:37,280 So we want to make sure that we're actually standing on something. 109 00:07:37,280 --> 00:07:43,040 And we also want to make sure that the last material is not equal to the enum dot material dot air. 110 00:07:43,730 --> 00:07:49,880 If all of that checks out, then what we could do here is return true. 111 00:07:50,950 --> 00:07:53,740 Otherwise we'll just return false. 112 00:07:54,590 --> 00:07:59,360 So this function is going to verify that a our humanoids walk speed is greater than zero. 113 00:07:59,390 --> 00:08:01,430 The humanoid is actually moving. 114 00:08:01,430 --> 00:08:08,060 The humanoid actually has a speed that they are moving with, and that they're actually standing on 115 00:08:08,060 --> 00:08:09,680 a particular surface. 116 00:08:09,980 --> 00:08:13,970 And if that's true, then what we could do is play a sound. 117 00:08:13,970 --> 00:08:19,700 So what we're going to do is we're going to get our footstep sound, and we're going to set the sound 118 00:08:19,730 --> 00:08:24,770 ID equal to whatever gets returned from our get material function. 119 00:08:24,770 --> 00:08:28,850 So we can call our get material function to get a particular sound ID. 120 00:08:29,630 --> 00:08:35,810 So that means what we need to do in this function is that we need to get a table from our footstep module, 121 00:08:35,810 --> 00:08:37,550 which is the sound table. 122 00:08:38,150 --> 00:08:41,180 So we can do footstep module. 123 00:08:41,180 --> 00:08:45,200 And there's a function in there called get table from material. 124 00:08:45,320 --> 00:08:49,760 And the material we want to pass is the last material we were standing on. 125 00:08:50,240 --> 00:08:54,110 And then after that we can get a random sound out of this table. 126 00:08:54,110 --> 00:08:59,180 So we could do random sound is equal to footstep module. 127 00:08:59,180 --> 00:09:01,940 And there's a function in there to allow us to get a random sound. 128 00:09:01,940 --> 00:09:04,970 And all we need to do is pass this sound table. 129 00:09:05,090 --> 00:09:07,730 And then after that we can return this sound ID. 130 00:09:09,200 --> 00:09:15,440 Now that we have returned the sound ID and we set it on our footstep sound instance, then what we could 131 00:09:15,440 --> 00:09:22,340 do is we could set footstep sound dot volume, make sure it is equal to the volume variable, and then 132 00:09:22,340 --> 00:09:25,100 we can just go ahead and play the footstep sound. 133 00:09:26,030 --> 00:09:29,960 So now it means we should be able to test whether or not our player has footsteps. 134 00:09:29,960 --> 00:09:33,230 So if we go into playtesting our game. 135 00:09:35,890 --> 00:09:39,940 And we spawn in here if I walk on this surface. 136 00:09:40,420 --> 00:09:42,490 As you can see, we here sand. 137 00:09:43,810 --> 00:09:47,320 If I walk on to the spawn plates, we should hear a different sound. 138 00:09:49,210 --> 00:09:49,870 Perfect. 139 00:09:50,740 --> 00:09:54,790 Now, if I go ahead and increase the speed of my player's character. 140 00:09:54,790 --> 00:10:00,790 So if I go to my humanoid and actually let me do this on the server so it replicates, if I go to my 141 00:10:00,790 --> 00:10:08,950 character, go to my humanoid, and I increase my walk speed to something, let's say like, uh, 24, 142 00:10:08,980 --> 00:10:14,200 then if I go back to my client, the footstep, uh, sound rate should be increased. 143 00:10:14,200 --> 00:10:15,550 So now if I walk around. 144 00:10:18,560 --> 00:10:22,640 As you can see, the footsteps are playing much more frequently than previously. 145 00:10:24,220 --> 00:10:24,700 All right. 146 00:10:24,700 --> 00:10:26,710 And that's all we needed to do in this lecture. 147 00:10:26,710 --> 00:10:31,090 In the next lecture, we're going to continue adding some neat functionality to our players character. 148 00:10:31,090 --> 00:10:32,620 So I'll go ahead and see you there.